package com.bestcem.xm.user.util.business;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.componet.redis.service.RedisService;
import com.bestcem.xm.user.constant.UserCacheKeyConstant;
import com.bestcem.xm.user.service.dto.redis.FastCreateUserDTO;
import com.bestcem.xm.user.service.dto.redis.UserLoginSessionDTO;
import com.bestcem.xm.user.service.dto.redis.UserRegisterDTO;
import com.bestcem.xm.user.service.dto.user.UserDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 业务缓存工具类
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/15 20:55
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Component
public class BusinessCacheUtil {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserCacheUtil userCacheUtil;

    /**
     * 生成sessionId 统一规则
     *
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 19:31
     */
    public String genSessionId() {
        return UUID.randomUUID().toString();
    }

    /**
     * 缓存微信用户信息
     * userRegisterDTO 用户信息
     *
     * @return java.lang.String
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/15 20:36
     */
    public String cacheWxUserInfo(UserRegisterDTO userRegisterDTO) {
        String sessionId = genSessionId();
        redisService.set(userCacheUtil.buildKey(UserCacheKeyConstant.REGISTER_INFO_AND_SESSION_ID, sessionId),
                JSON.toJSONString(userRegisterDTO), 2, TimeUnit.HOURS);
        return sessionId;
    }

    /**
     * 获取微信用户缓存信息
     *
     * @param sessionId sessionId
     * @return com.bestcem.xm.user.service.dto.redis.UserRegisterDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/15 20:59
     */
    public UserRegisterDTO getWxUserCacheInfo(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            return null;
        }
        return userCacheUtil.get(userCacheUtil.buildKey(UserCacheKeyConstant.REGISTER_INFO_AND_SESSION_ID, sessionId),
                UserRegisterDTO.class);
    }

    /**
     * 删除用户注册缓存信息
     *
     * @param sessionId
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/18 18:41
     */
    public void delWxUserCacheInfo(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            return;
        }
        userCacheUtil.delete(UserCacheKeyConstant.REGISTER_INFO_AND_SESSION_ID, sessionId);
    }

    /**
     * 缓存手机号快速注册用户信息
     *
     * @param fastCreateUserDTO 手机号快速注册用户信息
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 19:31
     */
    public String cacheFastCreateUser(FastCreateUserDTO fastCreateUserDTO) {
        return cacheFastCreateUser(genSessionId(), fastCreateUserDTO);
    }

    /**
     * 缓存手机号快速注册用户信息
     *
     * @param sessionId
     * @param fastCreateUserDTO
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/18 19:21
     */
    public String cacheFastCreateUser(String sessionId, FastCreateUserDTO fastCreateUserDTO) {
        userCacheUtil.setEx(userCacheUtil.buildKey(UserCacheKeyConstant.REGISTER_FAST_CREATE_USER, sessionId),
                JSON.toJSONString(fastCreateUserDTO));
        return sessionId;
    }

    /**
     * 获取缓存手机号快速注册用户信息
     *
     * @param sessionId
     * @return FastCreateUserDTO
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/19 19:24
     */
    public FastCreateUserDTO getFastCreateUser(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            return null;
        }
        return userCacheUtil.get(userCacheUtil.buildKey(UserCacheKeyConstant.REGISTER_FAST_CREATE_USER, sessionId), FastCreateUserDTO.class);
    }

    /**
     * 删除缓存手机号快速注册用户信息
     *
     * @param sessionId
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/19 19:24
     */
    public void delFastCreateUser(String sessionId) {
        userCacheUtil.delete(UserCacheKeyConstant.REGISTER_FAST_CREATE_USER, sessionId);
    }

    /**
     * 缓存用户快速登录信息
     *
     * @param userLoginSessionDTO
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 19:31
     */
    public String cacheFastLoginUser(UserLoginSessionDTO userLoginSessionDTO) {
        return cacheFastLoginUser(genSessionId(), userLoginSessionDTO);
    }

    /**
     * 缓存用户快速登录信息
     *
     * @param sessionId
     * @param userLoginSessionDTO
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 19:31
     */
    public String cacheFastLoginUser(String sessionId, UserLoginSessionDTO userLoginSessionDTO) {
        userCacheUtil.setEx(userCacheUtil.buildKey(UserCacheKeyConstant.USER_FAST_TOKEN, sessionId),
                JSON.toJSONString(userLoginSessionDTO));
        return sessionId;
    }

    /**
     * 获取用户快速登录信息缓存
     *
     * @param sessionId
     * @return UserLoginSessionDTO
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 19:31
     */
    public UserLoginSessionDTO getFastLoginUser(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            return null;
        }
        return userCacheUtil.get(userCacheUtil.buildKey(UserCacheKeyConstant.USER_FAST_TOKEN, sessionId), UserLoginSessionDTO.class);
    }

    /**
     * 校验用户是否被锁定
     *
     * @param orgCode 公司编码
     * @param userDTO 用户dto
     * @return
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/3/4 14:30
     */
    public ServiceResult<Void> checkUserLocked(String orgCode, UserDTO userDTO) {
        Boolean userLockFlag = userCacheUtil.hasKey(userCacheUtil
                .buildKey(UserCacheKeyConstant.USER_LOCKED_FLAG, orgCode, userDTO.getUserName(), userDTO.getPhone(), userDTO.getEmail()));
        if (Boolean.TRUE.equals(userLockFlag)) {
            return ServiceResult.fail(ResourceErrorCodeEnum.USER_LOCKED, "user has been locked");
        }
        return ServiceResult.success();
    }

    /**
     * 锁定用户
     *
     * @param orgCode 公司编码
     * @param userDTO 用户dto
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 17:53
     */
    public void cacheUserLocked(String orgCode, UserDTO userDTO) {
        userCacheUtil.setEx(userCacheUtil.buildKey(UserCacheKeyConstant.USER_LOCKED_FLAG,
                orgCode, userDTO.getUserName(), userDTO.getPhone(), userDTO.getEmail()), NumberUtils.INTEGER_ONE, 60L * 30);
    }

    /**
     * 清除锁定用户
     *
     * @param orgCode 公司编码
     * @param userDTO 用户dto
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/17 17:53
     */
    public void delUserLocked(String orgCode, UserDTO userDTO) {
        userCacheUtil.delete(UserCacheKeyConstant.USER_LOCKED_FLAG, orgCode, userDTO.getUserName(), userDTO.getPhone(), userDTO.getEmail());
    }
}
